package com.yanggu.mapreduce.mapjoin;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.net.URI;

/**
 * @version V1.0
 * @author: YangGu
 * @date: 2020/2/8 14:16
 * @description:
 */
public class MapJoinDriver {

    public static void main(String[] args) throws Exception {

        //0. 设置输入和输出的文件的路径
        args = new String[]{"input/mapjoin/order.txt", "output/mapjoin"};

        //1. 获取job对象
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        //2. 设置driver、mapper
        job.setJarByClass(MapJoinDriver.class);
        job.setMapperClass(MapJoinMapper.class);

        //3. 设置map的输出的key和value的类型
        job.setMapOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        //4. 设置最终输出的key和value的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        //5. 设置reduce的数量为0。表示不需要reduce阶段
        job.setNumReduceTasks(0);

        //6. 设置缓存的文件
        job.addCacheFile(new URI("input/mapjoin/pd.txt"));

        //7. 设置输出和输出的路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //8. 提交job
        boolean result = job.waitForCompletion(true);

        System.exit(result ? 0 : 1);
    }

}
